<?php
class currency extends head {
 function currency() {
  $this->table = "currency";
  $this->getmetadata();
  $this->maintainhist = true;
  $this->header[2] = "";
  $_SESSION[$this->table] = $this;
 }
 function showform() {
  echo '<head>';
  echo SCRIPTS;
  echo '</head>';
	echo "<body onload='document.form.f0.focus()'>";
  echo "<h1>Currency Setup</h1>"; 
  echo "<form name='form' method=GET>";
  echo "<input type=hidden name=res value=''>";
  echo "<table>";
  echo $this->htmlrow('0','Code');
  echo $this->htmlrow('1','Description');
  echo $this->htmlrow('5','Character');
  echo $this->htmlrow('6','Rate');
  echo "</table>";
  echo '<input class=bt accesskey=A type=submit name=sub value=Apply>';
  echo '<input class=bt accesskey=S type=submit name=sub value=Save>';
  echo '<input class=bt type=submit name=sub value=Delete>';
	echo '<button class=bt onclick=rest()><span>Restart</span></button>'; 
  echo "</form>"; 
 }
 function applyfromrequest() {
  $this->valid = true;
  // Goes through request. Applies if legal, otherwise error.
  $this->valids = array_fill(0,$this->atts,'');

  if (isset($_REQUEST['f0']) && $this->header[2] == '0') {
   $size = $this->metadata[0]->length ;  
   $f = $_REQUEST['f0'];

   if(preg_match('/^[A-Z]{2,5}$/',$f)) {
    $exists = freaditem($this->table,$f) ;
    if ($exists == "") {
     $this->header[0] = $f;
    } else {
     $this->valids[0] = "That ID is already in use for ".$exists[1];
    }
   } else {
    $this->valids[0] = "Please enter a three-to-five letter id e.g. NZD.";
   }
  }


  $i =1 ;
	if ($f=@$_REQUEST["f$i"]) {
   // name. Allowable characters A-Za-z-space apostrophe
   $f = $_REQUEST["f$i"];
   if(preg_match('/^[a-z A-Z\'\.-]{2,45}$/',$f)) {
    $this->header[$i] = $f; 
   } else {
    $this->valids[$i] = "Please enter a valid name";
   }
  }

  $i = 5 ;
	if ($f=@$_REQUEST["f$i"]) {
   // Exchange Rate
   $f = $_REQUEST["f$i"];
   if(strlen($f) == 1) {
    $this->header[$i] = $f; 
   } else {
    $this->valids[$i] = "Enter the rate character";
   }
  }

  $i = 6 ;
	if ($f=@$_REQUEST["f$i"]) {
   // Exchange Rate
   $f = $_REQUEST["f$i"];
   if(preg_match('/^[0-9]{1,6}.[0-9]{1,6}$/',$f)) {
    $this->header[$i] = $f; 
   } else {
    $this->valids[$i] = "Please enter an exchange rate max six decimal places";
   }
  }

 
  $this->valid = every($this->valids,"");
  if ($this->valid) {
   if ($this->header[0] == "") $this->valids[0] = "Incomplete";
   if ($this->header[1] == "") $this->valids[1] = "Incomplete";
   $this->valid = every($this->valids,"");   
  }
   
 }
}
?>